package net.zoneland.knowledge.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.zoneland.knowledge.constant.Constants;
import net.zoneland.knowledge.dao.ResourceMapper;
import net.zoneland.knowledge.model.Resource;
import net.zoneland.knowledge.model.Role;
import net.zoneland.knowledge.model.TreeNode;
import net.zoneland.knowledge.service.ResourceService;
import net.zoneland.knowledge.utils.TreeBuilder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * .
 *
 * @author: bcc
 * 2021/6/4
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public Resource getById(final String id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Resource> findResourceByRoles(final List<Role> roles) {
        return getResourceList(roles,null);
    }

    @Override
    public List<TreeNode<Resource>> findMenuByRoles(final List<Role> roles) {
        List<Resource> resourceList = getResourceList(roles, null);
        Collections.sort(resourceList);
        TreeNode<Resource> top = TreeBuilder.createTree(resourceList, Constants.RESOURCE_ROOT_PARENT);
        return top.getChildren();
    }

    /**
     * 根据角色列表获取资源列表
     *
     * @param roles
     *            角色列表
     * @param removeType
     *            去除的资源类型,null为不需要去除
     * @return
     */
    private List<Resource> getResourceList(List<Role> roles, String removeType) {
        List<Resource> resourceList = new ArrayList<Resource>(30);
        Map<String, String> existsMap = new HashMap<String, String>(30);
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                String roleId = role.getRoleId();
                List<Resource> resources = resourceMapper.getListByRoleId(roleId);
                removeDuplicate(resourceList, resources, existsMap, removeType);
            }
        }
        return resourceList;
    }

    /**
     * 去除重复资源
     *
     * @param resourceList
     *            去重的资源列表
     * @param resources
     *            待加入的资源列表
     * @param existsMap
     *            已加入的资源Map，用于判断
     * @param removeType
     *            去除的资源类型,null为不需要去除
     */
    private void removeDuplicate(List<Resource> resourceList, List<Resource> resources, Map<String, String> existsMap,
                                 String removeType) {
        if (resources != null && resources.size() > 0) {
            for (Resource resource : resources) {
                String resId = resource.getId();
                if (StringUtils.isBlank(removeType) || !removeType.equals(resource.getResourceType())) {
                    if (!existsMap.containsKey(resId)) {
                        resourceList.add(resource);
                        existsMap.put(resId, resId);
                    }
                }
            }
        }
    }

}
